package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.AuditNode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/7 15:46
 * @Description:
 */
@Repository("auditNodeDao")
public class AuditNodeDao implements CrudDao<AuditNode> {

    private final static Logger logger = LoggerFactory.getLogger(AuditNodeDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String nodeName = queryHash.get("nodeName");
        if (!StringUtils.isEmpty(nodeName)) {
            conditionHash.put("(s.nodeName like ?{paramIndex}) ", "%" + nodeName + "%");
        }
        String parentId = queryHash.get("parentId");
        if (!StringUtils.isEmpty(parentId)) {
            conditionHash.put("(s.parentId = ?{paramIndex}) ", StringConverters.null2Long(parentId));
        }
        String flowId = queryHash.get("flowId");
        if (!StringUtils.isEmpty(flowId)) {
            conditionHash.put("(s.auditFlow.id = ?{paramIndex}) ", StringConverters.null2Long(flowId));
        }
        String nodeId = queryHash.get("id");
        if (!StringUtils.isEmpty(nodeId)) {
            conditionHash.put("(s.id = ?{paramIndex}) ", StringConverters.null2Long(nodeId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from AuditNode s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<AuditNode> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from AuditNode s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<AuditNode> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, AuditNode.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public AuditNode findModel(Long id) {
        return entityManager.find(AuditNode.class, id);
    }

    @Override
    public Integer add(AuditNode model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("AuditNodeDaoImpl添加auditNodeInfo成功！");
        return 1;
    }

    @Override
    public Integer update(AuditNode model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        AuditNode auditNode = entityManager.find(AuditNode.class, id);
        entityManager.remove(auditNode);
        return 1;
    }

    @Override
    public Integer deleteLogic(AuditNode model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
